<template>
	<div class="box">
		<div class="title">
			<span class="tag"></span>
			报价总览
		</div>
		<div class="">
			<FormItems
				ref="form"
				v-model="calcSnapshoot"
				:form="formList"
				:state="state"
				labelWidth="100px"></FormItems>
		</div>
	</div>
</template>

<script>
	import FormItems from '@/views/finance/manage/formItems/index.vue';
	export default {
		components: { FormItems },
		props: {
			value: {
				required: false,
				default: () => {
					return {};
				},
			},
			state: {
				required: false,
			},
			type: {
				type: String,
				required: false,
			},
		},
		data() {
			return {};
		},
		inject: ['details_forecast', 'details_config'],
		computed: {
			formData: {
				get() {
					return this.value;
				},
				set(val) {
					this.$emit('input', val);
				},
			},
			config() {
				return this.details_config();
			},
			details() {
				return this.details_forecast();
			},
			formList() {
				return [
					{
						label: 'APR',
						// show: !(
						// 	this.config?.businessSource == '1' &&
						// 	this.details.rentType_raw == '1'
						// ),
						format: row => {
							let apr = this.notEmpty(row.apr)
								? row.apr
								: row.pricingIrr;
							let price = this.numberRide(apr * 1, 100);
							return Number.isFinite(price)
								? this.format_thousand(price?.toFixed(2)) + '%'
								: '-';
						},
					},
					{
						label: 'IRR',
						format: row => {
							let price = this.numberRide(
								row?.cashFlowCalcResult?.irr * 1,
								100,
							);
							return Number.isFinite(price)
								? this.format_thousand(price?.toFixed(2)) + '%'
								: '-';
						},
					},
					{
						label: '含资金成本IRR',
						// show:
						//     this.config?.businessSource == "1" &&
						//     this.details.rentType_raw == "1",
						format: row => {
							let price = this.numberRide(
								row?.cashFlowCalcResult1?.irr * 1,
								100,
							);
							return Number.isFinite(price)
								? this.format_thousand(price?.toFixed(2)) + '%'
								: '-';
						},
					},
					{
						label: '月费率',
						// show: !(
						//     this.config?.businessSource == "1" &&
						//     this.details.rentType_raw == "1"
						// ),
						format: row => {
							let price = this.numberRide(
								row?.rentCalcResult?.leaseRatePerPeriod * 1,
								100,
							);
							return Number.isFinite(price)
								? this.format_thousand(price?.toFixed(2)) + '%'
								: '-';
						},
					},
					{
						label: '总费率',
						// show: !(
						//     this.config?.businessSource == "1" &&
						//     this.details.rentType_raw == "1"
						// ),
						format: row => {
							let price = this.numberRide(
								row?.rentCalcResult?.totalLeaseRate * 1,
								100,
							);
							return Number.isFinite(price)
								? this.format_thousand(price?.toFixed(2)) + '%'
								: '-';
						},
					},
				];
			},
			calcSnapshoot() {
				if (
					this.formData.calcSnapshoot &&
					typeof this.formData.calcSnapshoot == 'string'
				) {
					return JSON.parse(this.formData.calcSnapshoot) || {};
				} else {
					return this.formData.calcSnapshoot || {};
				}
			},
			calcSnapshoot: {
				get() {
					if (
						this.formData.calcSnapshoot &&
						typeof this.formData.calcSnapshoot == 'string'
					) {
						return JSON.parse(this.formData.calcSnapshoot) || {};
					} else {
						return this.formData.calcSnapshoot || {};
					}
				},
				set(val) {
					this.formData = val;
				},
			},
		},
		created() {},
		mounted() {},
		methods: {
			valid() {
				return new Promise(async resolve => {
					let result = true;
					let refs = Object.values(this.$refs);
					for (let index = 0; index < refs?.length; index++) {
						const ref = refs[index];
						if (
							Object.prototype.toString.call(ref) ==
							'[object Array]'
						) {
							for (let i = 0; i < ref.length; i++) {
								const element = ref[i];
								if (element?.valid) {
									let rc = await element?.valid?.();
									result = result && rc;
								}
								if (element?.validate) {
									let rf = await this._valid(ref);
									result = result && rf;
								}
								if (element?.form_validate) {
									let rf = await this._valid(
										ref,
										'form_validate',
									);
									result = result && rf;
								}
							}
						} else {
							if (ref?.valid) {
								let rc = await ref?.valid?.();
								result = result && rc;
							}
							if (ref?.validate) {
								let rf = await this._valid(ref);
								result = result && rf;
							}
							if (ref?.form_validate) {
								let rf = await this._valid(
									ref,
									'form_validate',
								);
								result = result && rf;
							}
						}
					}

					resolve(result);
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		border-radius: 8px;
		background-color: #fff;
		.title {
			font-size: 14px;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			color: #333333;
			line-height: 22px;
			padding-bottom: 15px;
			// border-bottom: 1px solid #e9e9e9;
			display: flex;
			align-items: center;
			.tag {
				width: 8px;
				height: 8px;
				background-color: #1890ff;
				border-radius: 100%;
				margin-right: 10px;
			}
			.price {
				font-size: 14px;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #1890ff;
				line-height: 20px;
				margin-left: 20px;
			}
		}
	}
	::v-deep .el-input-number {
		width: 100%;
		.el-input-number__decrease,
		.el-input-number__increase {
			display: none;
		}
		input {
			width: 100%;
			padding: 5px;
			text-align: left;
		}
	}
</style>
